/*
 ============================================================================
 Name		: FbTabitem.cpp
 Author	  : ceze
 Version	 :
 Copyright   : Your copyright notice
 Description : CFbTabitem implementation
 ============================================================================
 */
#include <EIKENV.H>			//link eikcore.lib 
#include <stringloader.h> 	//link CommonEngine.lib 
#include "constdefined.hrh"
#include "FbTabitem.h"
#include "Flashbox4.mbg"

CFbTabitem::CFbTabitem ()
{
	m_isFocused = EFalse;
	m_pBack = NULL;
}

/**
 * 析构函数
 */
CFbTabitem::~CFbTabitem ()
{
	delete m_pImage;
	delete m_pImageMask;
	delete m_pTitle;
	delete m_pBack;
}

/**
 * Two-phased constructor.
 * aItemId:tab id
 * aResFileName:资源路径
 * aResImageId:图标资源id
 * aResImageMaskId:mask图标资源id
 * aResTextId:文字资源id
 */
CFbTabitem* CFbTabitem::NewLC (const CCoeControl *pParent,  TInt aItemId, const TDesC& aResFileName, const TInt& aResImageId, const TInt& aResImageMaskId, const TInt& aResBackId, const TRect& aRect, const TInt & aResTextId )
{
	CFbTabitem* self = new (ELeave) CFbTabitem;
	CleanupStack::PushL (self );
	self->ConstructL (pParent, aItemId, aResFileName, aResImageId,
			aResImageMaskId, aResBackId, aRect, aResTextId );
	return self;
}

/**
 * Two-phased constructor.
 * aItemId:tab id
 * aResFileName:资源路径
 * aResImageId:图标资源id
 * aResImageMaskId:mask图标资源id
 * aResTextId:文字资源id
 */
CFbTabitem* CFbTabitem::NewL (const CCoeControl *pParent,  TInt aItemId, const TDesC& aResFileName, const TInt& aResImageId, const TInt& aResImageMaskId, const TInt& aResBackId, const TRect& aRect, const TInt & aResTextId )
{
	CFbTabitem* self = CFbTabitem::NewLC (pParent, aItemId, aResFileName,
			aResImageId, aResImageMaskId, aResBackId, aRect, aResTextId );
	CleanupStack::Pop (); // self;
	return self;
}

/**
 * 实体构造函数
 */
void CFbTabitem::ConstructL (const CCoeControl *pParent, TInt aItemId, const TDesC& aResFileName, const TInt& aResImageId, const TInt& aResImageMaskId, const TInt& aResBackId, const TRect& aRect, const TInt& aResTextId )
{
	CreateWindowL (pParent );
	//SetContainerWindowL(*pParent);
	m_itemId = aItemId;
	//载入图片资源
	m_pImage
			= (CEikonEnv::Static ())->CreateBitmapL (aResFileName, aResImageId );
	m_pImageMask = (CEikonEnv::Static ())->CreateBitmapL (aResFileName,
			aResImageMaskId );

	m_backResId = aResBackId;
	m_pBack = (CEikonEnv::Static ())->CreateBitmapL (aResFileName, m_backResId );
	//标题文字
	if (aResTextId != 0 )
		m_pTitle = StringLoader::LoadL (aResTextId );

	// Set the windows size
	SetRect (aRect );
	// Activate the window, which makes it ready to be drawn
	ActivateL ();
}

/**
 * 绘制图形
 */
void CFbTabitem::Draw (const TRect& /*aRect*/) const
{
	CWindowGc& gc = SystemGc ();
	// Gets the control's extent
	TRect drawRect (Rect () );
	
	gc.SetPenStyle (CGraphicsContext::ENullPen );
	gc.SetBrushColor (m_backColor );
	gc.SetBrushStyle (CGraphicsContext::ESolidBrush );
	gc.Clear (drawRect );
	
	
	TSize tabSize (m_pBack->SizeInPixels () );
	TSize iconSize (m_pImage->SizeInPixels () );
	TRect iconRect (TPoint (drawRect.iTl.iX
			+ (tabSize.iWidth - iconSize.iWidth) / 2, drawRect.iTl.iY
			+ (tabSize.iHeight - iconSize.iHeight) / 2 ), iconSize );
	//底图
	if (m_isFocused && m_pBack )
		gc.DrawBitmap (drawRect, m_pBack );
	//tab图标
	if (m_pImage && m_pImageMask )
		gc.DrawBitmapMasked (iconRect, m_pImage, TRect (iconSize ),
				m_pImageMask, EFalse );
}

/**
 * From CoeControl, SizeChanged.
 * Called by framework when the view size is changed.
 */
void CFbTabitem::SizeChanged ()
{
	DrawNow ();
}

/**
 * From CoeControl,
 * 
 */
TKeyResponse CFbTabitem::OfferKeyEventL (const TKeyEvent &/*aKeyEvent*/, TEventCode /*aType*/)
{
	return EKeyWasNotConsumed;
}

/**
 * From CoeControl,CountComponentControls.
 */
TInt CFbTabitem::CountComponentControls () const
{
	return 0;
}

/**
 * From CCoeControl,ComponentControl.
 */
CCoeControl* CFbTabitem::ComponentControl (TInt /*aIndex*/) const
{
	return NULL;
}

/**
 * Tab id
 */
TInt CFbTabitem::TabId ()
{
	return m_itemId;
}

/**
 * tab title
 */
HBufC* CFbTabitem::TabTitle ()
{
	return m_pTitle;
}

/**
 * 激活
 */
void CFbTabitem::SetItemActiveL (TBool aActive )
{
	m_isFocused = aActive;	
	DrawNow ();
}

/**
 * 背景颜色
 */
void CFbTabitem::SetBackColor (const TRgb &aRgb )
{
	m_backColor = aRgb;
}

/**
 * 设置图标
 */
void CFbTabitem::SetTabImage(const TDesC& aResFileName, const TInt& aResImageId)
{
	if(m_pImage)
	{
		delete m_pImage;
		m_pImage = NULL;
	}
	m_pImage = (CEikonEnv::Static ())->CreateBitmapL (aResFileName, aResImageId );
}
